 var addP=new Array();
 $(document).ready(function(){
 


$('#tagresult').hide();
 $('#insertItem').hide();
 $('#searchbox').css("position", "absolute");

 var runningRequest = false;
    var request;
        $('input#search').keyup(function(e){
        $('#searchbox').html("");
        if($(this).val()=="")
            exit();

        if(runningRequest){
            request.abort();
        }
        runningRequest=true;

//         $('#tagresult').hide();
//            $('#searchItem').hide();
//            $('#insertItem').hide();
            
            var globaldata = null;
            $.getJSON("receipt_add.php?search="+$(this).val(),{}, function(data){
              
              // globaldata=data;
               // $('#searchbox').html(data.product.name+"<input type='hidden' name='id' value='"+data.product.id+"' >");
       
               
                var tblRow ='';
                                tblRow = tblRow + "<table border=1 id='tablesearch'> <tr>";
                                tblRow = tblRow + "<th>รหัสสินค้า</th>";
                                tblRow = tblRow + "<th >ชื่อสินค้า</th>";
                                tblRow = tblRow + "<th >ประเภท</th>";
                                tblRow = tblRow + "<th >จำนวนคงเหลือ</th>";
                               
                    
                $.each(data.out, function(i,result){
                    
                  if($.inArray('ckproduct'+result.id+'&'+result.Typeid, addP)<0){
                      
                        if(result.amount>0){
                                        tblRow = tblRow + '<tr onClick="addProduct('+result.id+','+result.Typeid+',0,2);"></td>';
                                        tblRow = tblRow + "<td >"+result.product_id+"</td>";
                                        tblRow = tblRow + "<td >"+result.name+"</td>";
                                        tblRow = tblRow + "<td >"+result.Typename+"</td>";
                                        tblRow = tblRow + "<td >"+result.amount+" "+result.nameunit+"</td>";
                    }
                  }
                   

		});
                tblRow = tblRow + "</table>";
                
               
                $('#searchbox').show();
                $("#searchbox").html(tblRow);
            });

       
            runningRequest=false;
        });

        $("body").not('#searchItem').click(function(){
             $('#searchbox').hide();

        });
        
//        $('#subok').click(function (){
//            var inputs = $('input'), empty = false;
//             inputs.each(function() {
//                            if ( $(this).val() == '' ) {
//                                empty = true;
//                                return;
//                            }
//                        });
//             var error = '';
//            if ( empty == true ) {
//                error = error + '- ใส่ข้อมูลไม่ครบ\n';
//            }
//            if(!$('input[name="receipt_type"]').attr('checked'))
//                error = error + '-ยังไมได้เลือกประเภทใบเสร็จ\n';
//            if($('input[name="product_id[]"]').length==0)
//                error = error +  '-ไม่เลือกสินค้า';
//            if(error != ''){
//                alert(error);
//                return false;
//            }
//
//
//        });
    });





    var countProduct=0;
    
    function addProduct(id,Typeid,Typeamount,action){

        //if($('#product'+id).length == 0)

        $(document).ready(function(){
            $.getJSON("receipt_add.php?addproduct="+id+"&Typeid="+Typeid,{}, function(data){
                $('#tagresult').show();
                 $('#insertItem').show();
                         var htmlOut = '<tr id="product'+data.s.id+'Typeid'+data.s.Typeid+'" ><td >'+data.s.product_id+'<input type="hidden" name="product_id[]" value="'+data.s.id+'"></td>';
                             htmlOut += '<td>'+data.s.name+'</td>';
                             htmlOut += '<td>'+data.s.Typename+'<input type="hidden" name="type_id[]" value="'+data.s.Typeid+'"></td>';
                             htmlOut += '<td>'+data.s.amount+'<input type="hidden" name="oldamount[]" value="'+data.s.oldamount+'"></td>';
                             htmlOut += '<td>'+data.s.ratio+' '+data.s.nameunit+'<input type="hidden" name="ratio[]" value="'+data.s.ratio+'"></td>';
                             htmlOut += '<td>'+data.s.asset+'<input type="hidden" name="asset[]" value="'+data.s.asset+'"></td>';
                             htmlOut += '<td><input type="text" size="6" name="buyprice[]" value="'+data.s.buyprice+'"></td>';
                             if(Typeamount=="")
                                 htmlOut += '<td><input type="text" size="6" name="amount[]"></td>';
                             else
                                htmlOut += '<td><input type="text" size="6" name="amount[]" value="'+Typeamount+'"><input type="hidden" name="amount_old[]" value="'+Typeamount+'"></td>';
                             if(action == 2) //add new
                                htmlOut += '<td><input type="button"  onClick="delProduct('+data.s.id+','+data.s.Typeid+')" value="ลบ"></a></td>';
                             else if(action == 1)//return
                                 htmlOut += '<td>คืนสินค้า</td>';
                             else
                                 htmlOut += '<td></td>';
                             htmlOut += '</tr>';
                                addP.push('ckproduct'+data.s.id+'&'+data.s.Typeid);
                        $('#searchresult').append(htmlOut);
                        countProduct++;
                        $('#countresult').html('<td colspan=8>จำนวน '+countProduct+' รายการ</td>');
                
              });



        });
            

    }
    function delProduct(id,Typeid){

        $('#product'+id+'Typeid'+Typeid).remove();
        countProduct--;
                        $('#countresult').html('<td colspan=8>จำนวน '+countProduct+' รายการ</td>');
    }